package com.njsoft.huanxin


import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View

import kotlinx.android.synthetic.main.activity_login.*
import com.hyphenate.chat.EMClient
import com.hyphenate.EMCallBack


/**
 * A login screen that offers login via email/password.
 */
class LoginActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
    }

    fun OnClick(v :View){
        when(v){
            Login->{
                val mUserName=LoginUserName.text.toString().trim()
                val mPassword=LoginPassword.text.toString().trim()
                if(!TextUtils.isEmpty(mUserName)&&!TextUtils.isEmpty(mPassword)){
                    login(mUserName,mPassword)
                }
            }
            Register->{
                startActivity(Intent(this,RegisterActivity::class.java))
            }
        }
    }

    fun login( userName:String, password:String){
        EMClient.getInstance().login(userName, password, object : EMCallBack {
            //回调
            override fun onSuccess() {
                EMClient.getInstance().groupManager().loadAllGroups()
                EMClient.getInstance().chatManager().loadAllConversations()
                Log.d("main", "登录聊天服务器成功！")
                startActivity(Intent(this@LoginActivity,ChatActivity::class.java))

            }

            override fun onProgress(progress: Int, status: String) {

            }

            override fun onError(code: Int, message: String) {
                Log.d("main", "登录聊天服务器失败！${message}")
            }
        })
    }
}
